﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lesson12_code2 : MonoBehaviour
{
    //目标对象
    public Transform target;
    //相对头顶的偏移位置
    public float headOffsetH = 1;
    //倾斜角度
    public float angle = 45;
    //默认摄像机离观测点的距离
    public float dis = 5;
    
    //最小距离,最大距离
    public float minDis = 3;
    public float maxDis = 10;

    //看向对象的旋转速度
    public float lookAtSpeed = 2;
    //跟随对象的移动速度
    public float moveSpeed = 1;

    //当前摄像机应该在的位置
    public Vector3 nowPos;

    void Start()
    {
        
    }

    private Vector3 nowDir;
    void Update()
    {
        //实现鼠标中键滚动  来改变摄像机远近
        dis -= Input.GetAxis("Mouse ScrollWheel");
        dis = Mathf.Clamp(dis, minDis, maxDis);

        //计算目标对象头顶偏移位置
        nowPos = target.position + target.up * headOffsetH;
        //往后方偏移位置
        nowDir = Quaternion.AngleAxis(angle, target.right) * -target.forward;
        nowPos = nowPos + nowDir * dis;

        //this.transform.position = nowPos;
        this.transform.position = Vector3.Lerp(this.transform.position, nowPos, Time.deltaTime * moveSpeed);

        Debug.DrawLine(this.transform.position, target.position + target.up * headOffsetH);

        //缓动看向目标对象
        this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(-nowDir), Time.deltaTime * lookAtSpeed);
    }
}
